﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace SGArchiveSystem7x.Controllers.Destroy
{
    public class DestroyArchiveController : BaseController
    {
        string sControllerName = "Destroy";
        /// <summary>
        /// 可以销毁的档案信息
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            var n = this.VaildUser(sControllerName);
            if (n != null)
                return n;

            ///清除Page级的Session
            App_Code.CSession.Clear_PageSession(Session);

            ///先获取档案类型
            var list = Models.CComboboxModel.Create_ComboboxArchiveTree(true);
            ViewBag.Items = list;
            ///部门
            var list2 = Models.CComboboxModel.Create_ComboboxDepartment(true, false);
            ViewBag.Items2 = list2;
            return View();
        }
        /// <summary>
        /// 获取可以销毁的档案
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Get_ajaxIsDestoryArchive(int arid,string year,string  dep)
        {
            int count = 0;
            int YEAR = -1;
            string DEP = null;
            if (year != null && year != "")
                YEAR = int.Parse(year);
            if (dep != "全部")
                DEP = dep;
            List<string> list1 = new List<string>(3);
            list1.Add(arid.ToString());
            list1.Add(year);
            list1.Add(DEP);
            App_Code.CSession.Add_QueryWhere(Session, list1);
            var list = SGArchiveSystem7xDB.Linq.V_ArchiveBgqxMjDal.Get_Archive(out count, DEP, YEAR, arid, null, 0, SGArchiveSystem7x.App_Code.CWebCommons._iPageRows);
            var json = new
            {
                total = count,
                rows = list
            };
            return Json(json, JsonRequestBehavior.AllowGet);
        }
        [HttpPost]
        public ActionResult Get_ajaxIsDestoryArchivePager(int num, int size)
        {
            int count = 0;
            int iStart = (num - 1) * size;
            var listWhere = App_Code.CSession.Get_QueryWhere(Session);
            var list = SGArchiveSystem7xDB.Linq.V_ArchiveBgqxMjDal.Get_Archive(out count, listWhere[2], int.Parse(listWhere[1]), int.Parse(listWhere[0]), null, iStart, SGArchiveSystem7x.App_Code.CWebCommons._iPageRows);
            var json = new
            {
                total = count,
                rows = list
            };
            return Json(json, JsonRequestBehavior.AllowGet);
        }
        public ActionResult QC()
        {
            return View();
        }
        public ActionResult Get_ajaxDestoryArchive(string name,int status)
        {
            int count = 0;
            List<string> list1 = new List<string>(2);
            list1.Add(name);
            list1.Add(status.ToString());
            App_Code.CSession.Add_QueryWhere(Session, list1);
            var list = SGArchiveSystem7xDB.Linq.V_ArchiveBgqxMjDal.Get_DestoryArchive(out count,status, name,0, SGArchiveSystem7x.App_Code.CWebCommons._iPageRows);
            var json = new
            {
                total = count,
                rows = list
            };
            return Json(json, JsonRequestBehavior.AllowGet);
        }
        public ActionResult Get_ajaxDestoryArchivePager(int num, int size)
        {
            int iStart = (num - 1) * size;
            var listWhere = App_Code.CSession.Get_QueryWhere(Session);
            int count = 0;
            var list = SGArchiveSystem7xDB.Linq.V_ArchiveBgqxMjDal.Get_DestoryArchive(out count, int.Parse(listWhere[1]), listWhere[0], iStart, SGArchiveSystem7x.App_Code.CWebCommons._iPageRows);
            var json = new
            {
                rows = list.ToArray(),
                total = count
            };
            return Json(json, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 建立清册
        /// </summary>
        /// <param name="name"></param>
        /// <param name="dh"></param>
        /// <returns></returns>
        public int Add(string name,string[] dh)
        {
            SGArchiveSystem7xDB.V_User user = App_Code.CSession.Get_User(Session);
            SGArchiveSystem7xDB.T_DestroyArchive en = new SGArchiveSystem7xDB.T_DestroyArchive();
            en.DES_NAME = name;
            en.DES_CREATEUSERID = user.U_ID;
            en.DES_CREATEUSERNAME = user.U_RNAME;
            en.DES_DATE = DateTime.Now;
            en.DES_STATUS = 1;
            int id = SGArchiveSystem7xDB.Linq.V_ArchiveBgqxMjDal.Add_Destory(en);
            if (dh != null)
            {
                foreach (var n in dh)
                {
                    SGArchiveSystem7xDB.Linq.V_ArchiveBgqxMjDal.Modify_Archive(n,id);
                }
            }
            else
            {
                int count = 0;
                var listWhere = App_Code.CSession.Get_QueryWhere(Session);
                var list= SGArchiveSystem7xDB.Linq.V_ArchiveBgqxMjDal.Get_Archive(out count,listWhere[2],int.Parse(listWhere[1]),int.Parse(listWhere[0]));
                foreach (var n in list)
                {
                    SGArchiveSystem7xDB.Linq.V_ArchiveBgqxMjDal.Modify_Archive(n.DH,id);
                }
            }
            return 1;
        }
        public ActionResult Destory(int id)
        {
            ViewBag.ID = id;
            return View();
        }
        public string QRXH(int id,string name)
        {
            try
            {
                var file = Request.Files["file1"];
                var super = name;
                string filename = DateTime.Now.Ticks.ToString() + ".pdf";
                var fullPath = Path.Combine(StorageRoot, Path.GetFileName(filename));
                file.SaveAs(fullPath);
                //添加压缩
                //SGArchiveSystem7xCommons.SGZip.CompressFile(fullPath, Server.MapPath("~/Content/PDF/" + filename + ".sg"));
                //FileInfo file1 = new FileInfo(fullPath);
                //file1.Delete();
                SGArchiveSystem7xDB.T_DestroyArchive en = new SGArchiveSystem7xDB.T_DestroyArchive();
                en.DES_ID = id;
                en.DES_V01 = super.ToString();
                en.DES_V02 = filename;
                SGArchiveSystem7xDB.Linq.V_ArchiveBgqxMjDal.QRXH_Destory(en);
                return "1";
            }
            catch (Exception ex)
            {
                return "0";
            }
        }

        private string StorageRoot
        {
            get { return Path.Combine(Server.MapPath("~/Content/PDF")); }
        }
        public ActionResult QCArchive(int del)
        {
            ViewBag.DEL = del;
            return View();
        }
        /// <summary>
        /// 获取自己清册的档案
        /// </summary>
        /// <param name="arid"></param>
        /// <param name="del">清册的id</param>
        /// <returns></returns>
        public ActionResult Get_ajaxQCArchiveList(int del)
        {
            int count = 0;
            List<string> list1 = new List<string>(1);
            list1.Add(del.ToString());
            App_Code.CSession.Add_QueryWhere(Session, list1);
            var list = SGArchiveSystem7xDB.Linq.V_ArchiveBgqxMjDal.Get_Archive(out count,null,-1,-1,del, 0, SGArchiveSystem7x.App_Code.CWebCommons._iPageRows);
            var json = new
            {
                total = count,
                rows = list
            };
            return Json(json, JsonRequestBehavior.AllowGet);
        }
        [HttpPost]
        public ActionResult Get_ajaxQCArchiveListPager(int num, int size)
        {
            int iStart = (num - 1) * size;
            var listWhere = App_Code.CSession.Get_QueryWhere(Session);
            int count = 0;
            var list = SGArchiveSystem7xDB.Linq.V_ArchiveBgqxMjDal.Get_Archive(out count, null, -1, -1, int.Parse(listWhere[0]), iStart, SGArchiveSystem7x.App_Code.CWebCommons._iPageRows);
            var json = new
            {
                rows = list.ToArray(),
                total = count
            };
            return Json(json, JsonRequestBehavior.AllowGet);
        }
        public string IsKF(int del)
        {
            int count = 0;
            string result = "";
            var list = SGArchiveSystem7xDB.Linq.V_ArchiveBgqxMjDal.Get_Archive(out count, null, -1, -1, del, -1,-1);
            foreach (var nn in list)
            {
                if (nn.KFZT == 1)
                {
                    result = "1";//在库
                }
                else
                {
                    result = "2";//被申请、借出、已销毁
                    break;
                }
            }
            return result;
        }
    }
}